﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin.Authority
{
    public partial class Add : SZSLSystem.Web.UI.ManagePage
    {
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.Params["ParentID"]))
                {
                    this.lblParent.Text = "系统权限";
                }
                else
                {
                    this.lblParent.Text = new SZSLSystem.BLL.Authority().GetModel(Guid.Parse(Request.Params["ParentID"])).AuthorityName;
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //
                //权限
                //
                SZSLSystem.Model.Authority model = new SZSLSystem.Model.Authority();
                SZSLSystem.BLL.Authority bll = new SZSLSystem.BLL.Authority();
                Guid ID = Guid.NewGuid();
                if (bll.Exists(ID))
                {
                    JscriptPrint("操作权限编号重复，请重新操作！", "", "Error");
                    return;
                }
                model.ID = ID;
                model.AuthorityName = this.txtName.Text.Trim();
                model.Abbreviation = this.txtAbbreviation.Text.Trim();
                if (!string.IsNullOrEmpty(Request.Params["ParentID"]))
                {
                    model.ParentID = Guid.Parse(Request.Params["ParentID"]);
                    model.ClassLayer = new SZSLSystem.BLL.Authority().GetModel(Guid.Parse(Request.Params["ParentID"])).ClassLayer + 1;
                }
                else
                {
                    model.ParentID = Guid.Parse(AuthorityID);
                    model.ClassLayer = 1;
                }
                model.Label = int.Parse(this.rblLabel.SelectedValue);
                model.Description = this.txtDescription.Text.Trim();
                model.Status = int.Parse(this.rblIsLock.SelectedValue);
                model.Sort = int.Parse(this.txtSort.Text.Trim());
                bll.Add(model);
                //
                //保存日志
                //
                SaveLogs("添加操作权限成功！操作权限名称：" + this.txtName.Text, 0);

                JscriptPrint("添加操作权限成功啦！", "List.aspx", "Success");

            }
            catch(Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>添加操作权限失败啦！", "", "Error");
            }
        }
    }
}
